package com.xu.book.dao.impl;

import com.xu.book.bean.BookType;
import com.xu.book.dao.IBookTypeDao;
import com.xu.sys.utils.MyDbutils;
import com.xu.sys.utils.PageUtils;
import com.xu.sys.utils.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class BookTypeDaoimpl implements IBookTypeDao {
    @Override
    public List<BookType> listPage(PageUtils pageUtils) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from t_book_type limit ?,?";
        if(StringUtils.isNotEmpty(pageUtils.getKey())){
            sql = "select * from t_book_type where name like '%"+pageUtils.getKey()+"%' limit ?,? ";
        }
        // 计算 分页开始的位置
        int startNo = pageUtils.getStart();
        try {
            return queryRunner.query(sql, new BeanListHandler<BookType>(BookType.class),startNo,pageUtils.getPageSize());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int count(PageUtils pageUtils) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select count(1) from t_book_type ";
        if(StringUtils.isNotEmpty(pageUtils.getKey())){
            sql = "select count(1) from t_book_type where name like '%"+pageUtils.getKey()+"%'  ";
        }
        try {
            return queryRunner.query(sql, new ResultSetHandler<Integer>() {
                @Override
                public Integer handle(ResultSet resultSet) throws SQLException {
                    resultSet.next();
                    return resultSet.getInt(1);
                }
            });
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<BookType> list() {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from t_book_type ";
        try {
            return queryRunner.query(sql, new BeanListHandler<BookType>(BookType.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int save(BookType entity) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "insert into t_book_type(name,notes)values(?,?)";
        try {
            return queryRunner.update(sql,entity.getName(),entity.getNotes());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public BookType findById(int id) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from t_book_type where id =?";
        try {

            List<BookType> list = queryRunner.query(sql, new BeanListHandler<BookType>(BookType.class), id);
            if (list !=null && list.size() > 0){
                return list.get(0);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateById(BookType entity) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql ="update t_book_type set name = ?,notes = ? where id = ?";
        try {
            return queryRunner.update(sql,entity.getName(),entity.getNotes(),entity.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteById(int id) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql ="delete from t_book_type where id = ?";
        try {
            return queryRunner.update(sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
